﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MotionCS.Alarm
{
    public enum AlarmLevelE
    {
        /// <summary>
        /// 无
        /// </summary>
        None,
        /// <summary>
        /// 警告
        /// </summary>
        Warning,
        /// <summary>
        /// 错误
        /// </summary>
        Error,
        /// <summary>
        /// 致命
        /// </summary>
        Fatal,
    }


    public class AlarmItem
    {
        /// <summary>
        /// ErrorID
        /// </summary>
        public int ErrorID { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable { get; set; } = true;
        /// <summary>
        /// 是否报警
        /// </summary>
        public bool IsAlarm { get; set; }
        /// <summary>
        /// 报警消息
        /// </summary>
        public string Message { get; set; } = "";
        /// <summary>
        /// 报警等级
        /// </summary>
        public AlarmLevelE Level { get; set; }
        /// <summary>
        /// 是否报警回调
        /// </summary>
        public Func<bool> IsAlarmCallback;
        /// <summary>
        /// 报警结束回调
        /// </summary>
        public Action AlarmEndedCallback;
    }


    public class AlarmDetect
    {
        /// <summary>
        /// 报警回调
        /// </summary>
        public Action<string> LogoutCallback { get; set; }

        /// <summary>
        /// 报警模组
        /// </summary>
        private List<AlarmItem> alarmItems = new List<AlarmItem>();
        public List<AlarmItem> AlarmItems
        {
            get => alarmItems;
            set
            {
                alarmItems = value;
            }
        }
        /// <summary>
        /// 报警检测
        /// </summary>
        public AlarmDetect() 
        { 
            
        }
        
        /// <summary>
        /// 报警循环
        /// </summary>
        public void AlarmForeach()
        {
            alarmItems.ForEach(itm =>
            {
                if (itm.IsAlarmCallback != null)
                {
                    if (itm.IsAlarmCallback())
                    {
                        if (!itm.IsAlarm)
                        {
                            LogoutCallback?.Invoke($"{itm.Message}报警");
                            itm.IsAlarm = true;

                            var inf = new AlarmInfo()
                            {
                                ErrorCode = itm.ErrorID,
                                Message = itm.Message,
                                IsAlarm = true
                            };
                            AlarmDevice.Instance.AlarmReport(inf);
                            if (itm.Level == AlarmLevelE.Fatal)
                            {
                                AlarmDevice.Instance.AlarmItemsAdd(inf);
                            }
                        }
                    }
                    else
                    {
                        if (itm.IsAlarm)
                        {
                            LogoutCallback?.Invoke($"{itm.Message}清除");
                            itm.IsAlarm = false;

                            var inf = new AlarmInfo()
                            {
                                ErrorCode = itm.ErrorID,
                                Message = itm.Message,
                                IsAlarm = false
                            };
                            AlarmDevice.Instance.AlarmReport(inf);
                            if (itm.Level == AlarmLevelE.Fatal)
                            {
                                AlarmDevice.Instance.AlarmItemsRemove(itm.ErrorID);
                            }

                            itm.AlarmEndedCallback?.Invoke();
                        }
                    }
                }
            });
        }
        /// <summary>
        /// 严重故障
        /// </summary>
        /// <returns></returns>
        public bool IsFatal()
        {
            var Fatal = alarmItems.FindAll(itm => itm.IsAlarm && itm.Level == AlarmLevelE.Fatal);
            return Fatal.Count() > 0;
        }
        /// <summary>
        /// 普通故障
        /// </summary>
        /// <returns></returns>
        public bool IsError()
        {
            var Fatal = alarmItems.FindAll(itm => itm.IsAlarm && itm.Level == AlarmLevelE.Error);
            return Fatal.Count() > 0;
        }
        /// <summary>
        /// 是否报警
        /// </summary>
        /// <returns></returns>
        public bool IsAlarm()
        {
            var Fatal = alarmItems.FindAll(itm => itm.IsAlarm && itm.Level > AlarmLevelE.Warning);
            return Fatal.Count() > 0;
        }
    }
}
